local biz_name = "vb"
-- configure redis connector 
ConfigRedisConnectors[biz_name]={
    host = "127.0.0.1",
    -- cur: host = "192.168.2.28"
    port = 6379,
    unixpath = "/etc/redis/unixpath/redis.sock",
    db = NIL,
    user = NIL,
    passwd = NIL,
    queue = "queue_4"
}

local function _vb()
  return coroutine.create(function ()
    
    -- constants
    local REDIS_SEPERATOR = "\t"
    local NUM_SEMICOLON = 11
    local DEF_LOCATION = "0"
    local lpush_cmd_format = "lpush "..ConfigRedisConnectors[biz_name].queue.." %s "
    local hget_cmd_format = "hget "..ConfigRedisConnectors[config.HGET_MOBILE_REDIS].queue.." "
    local mobile_cookie_domain = config.http.mobilecookie.domain
    -- functions
    local local_config = config
    local local_reply = reply
    local local_request = request
    local local_luredis = luredis
    local os_time = os.time


    while true do
	while true do -- validate params 
          local_reply.pushHeader(local_config.SERVER_HEADER, local_config.http.servername)
          local_reply.pushHeader(local_config.CACHE_CONTROL_HEADER, "private, no-cache, no-cache=Set-Cookie, proxy-revalidate")
          local_reply.pushHeader(local_config.PRAGMA_HEADER, "no-cache")
          local_reply.pushHeader(local_config.EXPIRES_HEADER, "Wed, 17 Sep 1975 21:32:10 GMT")
          local_reply.pushHeader(local_config.CONTENT_TYPE_HEADER, "image/gif")
          local_reply.pushHeader(local_config.LAST_MODIFIED_HEADER, "Wed, 19 Apr 2000 11:43:00 GMT")
          local_reply.pushHeader("P3P", "CP=\"IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA\"")
          local_reply.setStatus(local_reply.status_type.ok)
          paraTable = local_request.getParameters_value(); -- default seperator = ";"
          if #paraTable == NUM_SEMICOLON+1 then -- table length = number of seperators + 1
	    break;
          end
          --print("Invalid params")
	  coroutine.yield(); -- return due to incorrect number of params
        end
	-- table        
        local headerTable = local_request.getHeaders_key_value();

        local user_agent = headerTable[local_config.USER_AGENT_HEADER]
        local referer = headerTable[local_config.REFERER_HEADER];
	-- cookie 
        local cookieString = headerTable[local_config.COOKIE_HEADER];
        local cookieTable = parseCookies(cookieString);

        -- Get guid: guid
        local guid = cookieTable[local_config.GUID_COOKIE];
        local create_cookie = cookieTable[local_config.CREATE_COOKIE]
        

        local create_time = os_time();
        local expire_time = local_config.EXPIRE_TIME+create_time;
        local ip = local_request.getIP()
	-- guid 
        if not guid then
            guid = create_guid(create_time, ip);
	    pushCookieHeader(local_config.GUID_COOKIE, guid, expire_time, "/", mobile_cookie_domain, false)
            pushCookieHeader(config.CREATE_COOKIE, create_time, expire_time, "/", mobile_cookie_domain, false)
        end
        -- get region
        local region = cookieTable[local_config.REGION_COOKIE];
        if not region then -- ask redis for region
            reply_type, region = util.redis_command(local_config.HGET_MOBILE_REDIS,false,
						    local_luredis.send_cmd,hget_cmd_format..c_inet_network(ip));
            if reply_type ~= local_luredis.reply_type.string then
              region = DEF_LOCATION
	    else
              pushCookieHeader(local_config.REGION_COOKIE, region, expire_time, "/", mobile_cookie_domain, false)
	    end
        end
        -- get mobile os version (mosv)
        local mosv = cookieTable[local_config.OS_MOBILE_COOKIE]
        if not mosv then -- ask mobile service for os
            local mbl_service_ = getMobileService();
	    if(mbl_service_ ~= nil) then
		osVersion = mbl_service_:parse_user_agent(userAgent)
		pushCookieHeader(local_config.OS_MOBILE_COOKIE, mosv, expire_time, "/", mobile_cookie_domain, false)                
	    end
	end

        local path = paraTable[NUM_SEMICOLON+1]
        paraTable[NUM_SEMICOLON+1] = nil -- path is set aside

        local_luredis.clear_cmd_buffer()
        local_luredis.append_cmd_buffer(
	  create_time, REDIS_SEPERATOR, ip, REDIS_SEPERATOR, "0", REDIS_SEPERATOR, 
          guid, REDIS_SEPERATOR, create_cookie, REDIS_SEPERATOR, region,REDIS_SEPERATOR,
          osVersion, REDIS_SEPERATOR, table.concat(paraTable,";"),REDIS_SEPERATOR,
          path,REDIS_SEPERATOR, referer, REDIS_SEPERATOR, user_agent)
        util.redis_command(biz_name,true,local_luredis.send_cmd_buffer,lpush_cmd_format);     
        coroutine.yield()        
    end
  end)
end

creators[biz_name] = function()
    f[biz_name] = _vb()
end
